package org.example.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.dto.ClassDto;
import org.example.dto.GradeDto;
import org.example.service.ClassService;
import org.example.service.TestService;
import org.example.utils.JsonUtils;
import org.example.vo.ClassVo;
import org.example.vo.GradeVo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Rongchang Wu
 * @version 1.0
 * @date 2021/12/8 22:41
 */
@RestController
public class ClassController {
    @Resource
    ClassService service;

    @GetMapping("/getClassList")
    public void getClassList(HttpServletResponse resp, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<ClassDto> classList = service.queryAllClass();
        classList.forEach(s -> System.out.println(s));
        Integer count = service.countClass();
        System.out.println(count);
        //以上调用service获取专业数据和数量

        JsonUtils.customJson(resp, classList, count);
    }

    @GetMapping("/getClassByDepartmentNameAndMajorNameAndGradeName")
    public void getClassByDepartmentNameAndMajorNameAndGradeName(HttpServletResponse resp, String departmentName, String majorName, String gradeName, Integer page, Integer limit) throws IOException {
        Page<Object> obj = PageHelper.startPage(page, limit);
        List<ClassDto> classList = service.queryClassByDepartmentAndMajorAndGrade(departmentName, majorName, gradeName);
        Integer count = service.countClassByDepartmentAndMajorAndGrade(departmentName, majorName, gradeName);

        JsonUtils.customJson(resp, classList, count);
    }

    @GetMapping(value = "/deleteClass", produces = "text/plain;charset=utf-8")
    public String deleteClass(String classId) {
        String[] classIds = classId.split(",");
        Integer status = 0;
        for (int i = 0; i < classIds.length; i++) {
            status += service.deleteClass(Integer.parseInt(classIds[i]));
        }

        return status > 0 ? "success" : "error";
    }

    @GetMapping(value = "/addClass", produces = "text/plain;charset=utf-8")
    public String addClass(ClassVo classVo) {
        System.out.println("进入/addClass");
        Integer status = service.addClass(classVo);

        return status > 0 ? "success" : "error";
    }

    @GetMapping(value = "/getClassById")
    public ClassDto getClassById(Integer classId) {
        System.out.println("classId= " + classId);
        ClassDto classDto = service.queryClassById(classId);

        return classDto;
    }

    @GetMapping(value = "/editClass")
    public String editClass(ClassVo classVo) {
        Integer status = service.updateClass(classVo);

        return status > 0 ? "success" : "error";
    }
}
